package org.misalen.web.database.repository.mysql;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.misalen.web.database.dao.mysql.impl.BaseMySqlDaoImpl;
import org.misalen.web.database.model.mysql.pro.ProDriverLocation;
import org.springframework.stereotype.Repository;

@Repository
public class ProDriverLocationRepository extends BaseMySqlDaoImpl<ProDriverLocation> {

	public List<ProDriverLocation> findAllLocation() {
		Session session = this.openSession();
		String sql = "SELECT A.userName,(select c.latitude "
				+ " from ProDriverLocation as c where c.driverUserId = A.primaryKey "
				+ " order by c.addTime desc limit 0,1) as latitude,(select c.longitude "
				+ " from ProDriverLocation as c where c.driverUserId = A.primaryKey "
				+ " order by c.addTime desc limit 0,1) as longitude FROM ProDriverUser A ";
		SQLQuery query = session.createSQLQuery(sql);
		query.setResultTransformer(new AliasToBeanResultTransformer(ProDriverLocation.class));
		@SuppressWarnings("unchecked")
		List<ProDriverLocation> list = query.list();
		closeSession(session);
		return list;
	}

}